package com.lg.partition56;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/4/16 9:38
 */
public class P5536核心城市 {

    private static final int N = 100010, M = 2 * N;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], depth = new int[N], maxd = new int[N];
    //path[i]表示i节点的上一个节点，dist[i]=maxd[i]-depth[i]
    private static int[] path = new int[N], dist = new int[N];
    private static int n, k, td = 0, tp = 0, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        k = Integer.parseInt(ss[1]);
        Arrays.fill(h, -1);
        for (int i = 0; i < n - 1; i++) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
            add(a, b);
            add(b, a);
        }
        writer.write(resolve() + "\n");
        writer.flush();
    }

    private static int resolve() {
        //先任选一点进行dfs找到直径的一个端点，再以这个端点进行一次dfs找到另一个直径端点，期间记录路径情况path，至此求出树的直径
        dfs1(1, -1);
        Arrays.fill(depth, 0);
        td = 0;
        dfs2(tp, -1);
        //根据找到的两个端点，利用path移动到最中间的端点，也就是树的直径的中点，以这个中点进行一次dfs，处理出新的depth和maxd，并对maxd[i]-depth[i]进行从大到小排序
        int mid = tp;
        for (int i = 1; i <= (depth[tp] + 1) / 2; i++) mid = path[mid];
        Arrays.fill(depth, 0);
        //以中心点进行一次dfs，找出depth和maxd
        dfs3(mid, -1);
        for (int i = 1; i <= n; i++) dist[i] = maxd[i] - depth[i];
        Arrays.sort(dist, 1, n + 1);
        int l = 1, r = n;
        while (l < r) {
            int t = dist[l];
            dist[l++] = dist[r];
            dist[r--] = t;
        }
        int ans = 0;
        for (int i = k + 1; i <= n; i++) ans = Math.max(ans, dist[i] + 1);
        return ans;
    }

    private static void dfs3(int u, int fa) {
        maxd[u] = depth[u];
        for(int i = h[u]; i != -1; i = ne[i]) {
            int y = e[i];
            if(y == fa) continue;
            depth[y] = depth[u] + 1;
            dfs3(y, u);
            maxd[u] = Math.max(maxd[u], maxd[y]);
        }
    }

    private static void dfs2(int u, int fa) {
        if(depth[u] > td){
            td = depth[u];
            tp = u;
        }
        for(int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if(v == fa) continue;
            depth[v] = depth[u] + 1;
            path[v] = u;
            dfs2(v, u);
        }
    }

    private static void dfs1(int u, int fa) {
        if(depth[u] > td) {
            td = depth[u];
            tp = u;
        }
        for(int i = h[u]; i != -1; i = ne[i]) {
            int y = e[i];
            if(y == fa) continue;
            depth[y] = depth[u] + 1;
            dfs1(y, u);
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
